<link rel="stylesheet" href="${request.contextPath}/css/suredy-tree.css">
<div class="container-fluid suredy-form" style="padding-top: 0">
	<div class="row">	
		<form name="form" id="form">
		<#if role??>
			<input type="hidden"  name="id" value="${role.id}" />
			<input type="hidden" name="resources" id="resources" value="" />
			<div class="form-group">
				<div class="input-group">
					<div class="input-group-addon">角色代码</div>
					<input type="text" class="form-control" name="name" readonly="readonly" value="${role.uniqueCode}" />
				</div>
			</div>	
			<div class="form-group">	
				<div class="input-group">
					<div class="input-group-addon">角色名称</div>
					<input type="text" class="form-control" name="name" readonly="readonly" value="${role.name}" />
				</div>
			</div>
		</#if>	
		    <div class="form-group">
				<div class="list-group" style="border-top: 1px solid #ddd; max-height:400px; overflow:auto;">
					<div class="suredy-tree normal" id="resource-tree" data-uri="resource/role?roleId=${role.id}" >加载中......</div>
				</div>
			</div>

			<div class="text-center">
				<button class="btn btn-info" id="btn-save" type="button">
					<i class="icon-save"></i>
					保存
				</button>
			</div>	
		</form>	
	</div>
</div>
<script src="${request.contextPath}/js/suredy-tree.js"></script>
<script type="text/javascript">
	$(document).ready(function() {
		var loadTree = function() {
			$('#resource-tree').tree($('#resource-tree').data('uri'), {checkbox:true});	
		};
		loadTree();	
	
		$('#btn-save').click(function() {
			var checked = $.suredy.tree.data($.suredy.tree.checked($('#resource-tree>.suredy-tree')));
			var data = "";
			
			if (checked) {
				if (jQuery.type(checked) === "array") {
					var temp = new Array();
					$.each(checked, function(){
						if (this.resourceId) {	
							temp[temp.length] = this.resourceId;
						}
					});
					data = temp.join('-');
				} else  if (jQuery.type(checked) === "object") {
					if (checked.resourceId) {
						data = checked.resourceId;
					}
				}
			}
			$('#resources').val(data);
			
			$.ajax({
				type : 'POST',//发送请求的方式
				url : 'role/permission-save.do',
				data : $('#form').serialize(),
				dataType : "json",//文件类型
				cache : false,
				timeout : 60000,//超时设置，单位为毫秒
				success : function(success) {
					if (!success) {
						alert('修改角色权限失败！');
					} else if (!success.success) {
						alert('修改角色权限失败！\n\n' + success.msg);
					} else {
						alert('修改角色权限成功！');
						$.suredy.loadContent('role/list.do');
						$.suredy.closeModal();
					}
				},
				error : function(a, b, c) {
					alert('服务器错误! ' + b);
				}
			});		
		});	
	});
</script>	
